<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
  <meta name="theme-color" content="#222">
  <meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

  <link rel="stylesheet" href="/css/main.css">



  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

  <script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {
      "hostname": "holidaypenguin.gitee.io",
      "root": "/",
      "images": "/images",
      "scheme": "Mist",
      "version": "8.2.2",
      "exturl": false,
      "sidebar": {
        "position": "right",
        "display": "always",
        "padding": 18,
        "offset": 12
      },
      "copycode": true,
      "bookmark": {
        "enable": false,
        "color": "#222",
        "save": "auto"
      },
      "fancybox": false,
      "mediumzoom": false,
      "lazyload": false,
      "pangu": false,
      "comments": {
        "style": "tabs",
        "active": null,
        "storage": true,
        "lazyload": false,
        "nav": null
      },
      "motion": {
        "enable": true,
        "async": true,
        "transition": {
          "post_block": "fadeIn",
          "post_header": "fadeInDown",
          "post_body": "fadeInDown",
          "coll_header": "fadeInLeft",
          "sidebar": "slideUpIn"
        }
      },
      "prism": false,
      "i18n": {
        "placeholder": "搜索...",
        "empty": "没有找到任何搜索结果：${query}",
        "hits_time": "找到 ${hits} 个搜索结果（用时 ${time} 毫秒）",
        "hits": "找到 ${hits} 个搜索结果"
      },
      "path": "/search.xml",
      "localsearch": {
        "enable": true,
        "trigger": "auto",
        "top_n_per_article": 1,
        "unescape": false,
        "preload": false
      }
    };
  </script>
  <meta property="og:type" content="article">
  <meta property="og:title" content="Transfer-Encoding、Content-Length、Content-Encoding、Accept-Encoding">
  <meta property="og:url" content="https://holidaypenguin.gitee.io/blob/2019-07-11-transfer-encoding,-content-length,-content-encoding,-accept-encoding/index.html">
  <meta property="og:site_name" content="HolidayPenguin">
  <meta property="og:locale" content="zh_CN">
  <meta property="article:published_time" content="2019-07-11T10:26:16.000Z">
  <meta property="article:modified_time" content="2019-07-11T10:26:16.000Z">
  <meta property="article:author" content="holidaypenguin">
  <meta property="article:tag" content="HTTP">
  <meta property="article:tag" content="Transfer-Encoding">
  <meta property="article:tag" content="Content-Length">
  <meta property="article:tag" content="Content-Encoding">
  <meta property="article:tag" content="Accept-Encoding">
  <meta name="twitter:card" content="summary">


  <link rel="canonical" href="https://holidaypenguin.gitee.io/blob/2019-07-11-transfer-encoding,-content-length,-content-encoding,-accept-encoding/">


  <script class="page-configurations">
    // https://hexo.io/docs/variables.html
    CONFIG.page = {
      sidebar: "",
      isHome: false,
      isPost: true,
      lang: 'zh-CN'
    };
  </script>
  <title>Transfer-Encoding、Content-Length、Content-Encoding、Accept-Encoding | HolidayPenguin</title>





  <noscript>
    <style>
      body {
        margin-top: 2rem;
      }

      .use-motion .menu-item,
      .use-motion .sidebar,
      .use-motion .post-block,
      .use-motion .pagination,
      .use-motion .comments,
      .use-motion .post-header,
      .use-motion .post-body,
      .use-motion .collection-header {
        visibility: visible;
      }

      .use-motion .header,
      .use-motion .site-brand-container .toggle,
      .use-motion .footer {
        opacity: initial;
      }

      .use-motion .site-title,
      .use-motion .site-subtitle,
      .use-motion .custom-logo-image {
        opacity: initial;
        top: initial;
      }

      .use-motion .logo-line {
        transform: scaleX(1);
      }

      .search-pop-overlay,
      .sidebar-nav {
        display: none;
      }

      .sidebar-panel {
        display: block;
      }
    </style>
  </noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner">
        <div class="site-brand-container">
          <div class="site-nav-toggle">
            <div class="toggle" aria-label="切换导航栏" role="button">
              <span class="toggle-line"></span>
              <span class="toggle-line"></span>
              <span class="toggle-line"></span>
            </div>
          </div>

          <div class="site-meta">

            <a href="/" class="brand" rel="start">
              <i class="logo-line"></i>
              <h1 class="site-title">HolidayPenguin</h1>
              <i class="logo-line"></i>
            </a>
          </div>

          <div class="site-nav-right">
            <div class="toggle popup-trigger">
              <i class="fa fa-search fa-fw fa-lg"></i>
            </div>
          </div>
        </div>



        <nav class="site-nav">
          <ul class="main-menu menu">
            <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
            <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
            <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
            <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
            <li class="menu-item menu-item-search">
              <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
              </a>
            </li>
          </ul>
        </nav>



        <div class="search-pop-overlay">
          <div class="popup search-popup">
            <div class="search-header">
              <span class="search-icon">
                <i class="fa fa-search"></i>
              </span>
              <div class="search-input-container">
                <input autocomplete="off" autocapitalize="off" maxlength="80" placeholder="搜索..." spellcheck="false" type="search" class="search-input">
              </div>
              <span class="popup-btn-close" role="button">
                <i class="fa fa-times-circle"></i>
              </span>
            </div>
            <div class="search-result-container no-result">
              <div class="search-result-icon">
                <i class="fa fa-spinner fa-pulse fa-5x"></i>
              </div>
            </div>

          </div>
        </div>

      </div>


      <div class="toggle sidebar-toggle" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
      </div>

      <aside class="sidebar">

        <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
          <ul class="sidebar-nav">
            <li class="sidebar-nav-toc">
              文章目录
            </li>
            <li class="sidebar-nav-overview">
              站点概览
            </li>
          </ul>

          <div class="sidebar-panel-container">
            <!--noindex-->
            <div class="post-toc-wrap sidebar-panel">
              <div class="post-toc animated">
                <ol class="nav">
                  <li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A6%82%E8%BF%B0"><span class="nav-number">1.</span> <span class="nav-text">概述</span></a></li>
                  <li class="nav-item nav-level-2"><a class="nav-link" href="#keep-alive"><span class="nav-number">2.</span> <span class="nav-text">keep-alive</span></a></li>
                  <li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%BB%E8%A6%81%E5%86%85%E5%AE%B9"><span class="nav-number">3.</span> <span class="nav-text">主要内容</span></a>
                    <ol class="nav-child">
                      <li class="nav-item nav-level-3"><a class="nav-link" href="#Content-Length"><span class="nav-number">3.1.</span> <span class="nav-text">Content-Length</span></a></li>
                      <li class="nav-item nav-level-3"><a class="nav-link" href="#Transfer-Encoding"><span class="nav-number">3.2.</span> <span class="nav-text">Transfer-Encoding</span></a></li>
                      <li class="nav-item nav-level-3"><a class="nav-link" href="#Accept-Encoding%E5%92%8CContent-Encoding"><span class="nav-number">3.3.</span> <span class="nav-text">Accept-Encoding和Content-Encoding</span></a></li>
                    </ol>
                  </li>
                  <li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%BE%E4%BE%8B%E8%AF%B4%E6%98%8E"><span class="nav-number">4.</span> <span class="nav-text">举例说明</span></a></li>
                </ol>
                </li>
                <li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8F%82%E8%80%83"><span class="nav-number"></span> <span class="nav-text">参考</span></a>
              </div>
            </div>
            <!--/noindex-->

            <div class="site-overview-wrap sidebar-panel">
              <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
                <p class="site-author-name" itemprop="name">holidaypenguin</p>
                <div class="site-description" itemprop="description"></div>
              </div>
              <div class="site-state-wrap site-overview-item animated">
                <nav class="site-state">
                  <div class="site-state-item site-state-posts">
                    <a href="/archives/">

                      <span class="site-state-item-count">138</span>
                      <span class="site-state-item-name">日志</span>
                    </a>
                  </div>
                  <div class="site-state-item site-state-categories">
                    <a href="/categories/">

                      <span class="site-state-item-count">26</span>
                      <span class="site-state-item-name">分类</span></a>
                  </div>
                  <div class="site-state-item site-state-tags">
                    <a href="/tags/">

                      <span class="site-state-item-count">234</span>
                      <span class="site-state-item-name">标签</span></a>
                  </div>
                </nav>
              </div>
              <div class="links-of-author site-overview-item animated">
                <span class="links-of-author-item">
                  <a href="https://github.com/holidaypenguin" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;holidaypenguin" rel="noopener" target="_blank"><i class="github fa-fw"></i>GitHub</a>
                </span>
                <span class="links-of-author-item">
                  <a href="mailto:songshipeng2016@gmail.com" title="E-Mail → mailto:songshipeng2016@gmail.com" rel="noopener" target="_blank"><i class="envelope fa-fw"></i>E-Mail</a>
                </span>
              </div>



            </div>
          </div>
        </div>
      </aside>
      <div class="sidebar-dimmer"></div>


    </header>


    <div class="back-to-top" role="button">
      <i class="fa fa-arrow-up"></i>
      <span>0%</span>
    </div>

    <noscript>
      <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
    </noscript>


    <div class="main-inner post posts-expand">





      <div class="post-block">



        <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
          <link itemprop="mainEntityOfPage" href="https://holidaypenguin.gitee.io/blob/2019-07-11-transfer-encoding,-content-length,-content-encoding,-accept-encoding/">

          <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
            <meta itemprop="image" content="/images/avatar.gif">
            <meta itemprop="name" content="holidaypenguin">
            <meta itemprop="description" content="">
          </span>

          <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
            <meta itemprop="name" content="HolidayPenguin">
          </span>
          <header class="post-header">
            <h1 class="post-title" itemprop="name headline">
              Transfer-Encoding、Content-Length、Content-Encoding、Accept-Encoding
            </h1>

            <div class="post-meta-container">
              <div class="post-meta">
                <span class="post-meta-item">
                  <span class="post-meta-item-icon">
                    <i class="far fa-calendar"></i>
                  </span>
                  <span class="post-meta-item-text">发表于</span>

                  <time title="创建时间：2019-07-11 18:26:16" itemprop="dateCreated datePublished" datetime="2019-07-11T18:26:16+08:00">2019-07-11</time>
                </span>
                <span class="post-meta-item">
                  <span class="post-meta-item-icon">
                    <i class="far fa-folder"></i>
                  </span>
                  <span class="post-meta-item-text">分类于</span>
                  <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                    <a href="/categories/HTTP/" itemprop="url" rel="index"><span itemprop="name">HTTP</span></a>
                  </span>
                </span>


                <span id="/blob/2019-07-11-transfer-encoding,-content-length,-content-encoding,-accept-encoding/" class="post-meta-item leancloud_visitors" data-flag-title="Transfer-Encoding、Content-Length、Content-Encoding、Accept-Encoding" title="阅读次数">
                  <span class="post-meta-item-icon">
                    <i class="far fa-eye"></i>
                  </span>
                  <span class="post-meta-item-text">阅读次数：</span>
                  <span class="leancloud-visitors-count"></span>
                </span>
              </div>

              <div class="post-description">
                <!-- more -->
              </div>
            </div>
          </header>




          <div class="post-body" itemprop="articleBody">
            <h2 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h2>
            <p>我们在日常进行HTTP请求的时候，会遇到请求头或者响应头中有<code>Transfer-Encoding</code>、<code>Content-Length</code>、<code>Content-Encoding</code>、<code>Accept-Encoding</code>这几个，难免会不知道各自的含义及使用场景，也可能会搞混，我们今天就简单的捋一下这几个Header。</p>
            <h2 id="keep-alive"><a href="#keep-alive" class="headerlink" title="keep-alive"></a>keep-alive</h2>
            <p>在讲解上面几个头之前，我们必须先了解一下<code>Persistent Connection</code>（持久连接或者长连接），<code>HTTP</code>持久连接这个机制，很大程度上提高了<code>HTTP</code>的性能，我们看下在引入持久连接之前，<code>HTTP连</code>接是什么样子：</p>
            <p>先进行<code>TCP</code>握手，然后进行数据传输，完成数据传输之后，释放连接。当网络内容复杂时候，会出现多次数据传输，那么就会造成多次频繁的创建<code>Socket</code>和释放，频繁的握手，就会出现延时，这个问题在引入持久连接得以解决。</p>
            <p>通过设置设置 <code>Connection</code>头部为<code>keep-alive</code>实现持久连接：</p>
            <p><code>keepalive connections</code>机制可以在数据传输完成之后仍然保持<code>TCP</code>连接，以备后用，当用户需要再次获取数据时，直接使用刚刚空闲下来的连接，无需再次握手，这样就解决了延时问题，提高了<code>HTTP</code>的性能。持久连接是<code>HTTP1.0</code>后来才引进的，到了<code>HTTP1.1</code>规定所有连接都必须是持久的，在<code>Header</code>上添加了<code>Connection</code>为<code>close</code>。在现代浏览器中，一般同时开启6～8个<code>keepalive connections</code>的<code>socket</code>连接，并保持一定的链路生命，当不需要时再关闭；而在服务器中，一般是由软件根据负载情况进行配置。</p>
            <h2 id="主要内容"><a href="#主要内容" class="headerlink" title="主要内容"></a>主要内容</h2>
            <h3 id="Content-Length"><a href="#Content-Length" class="headerlink" title="Content-Length"></a>Content-Length</h3>
            <p>我们通过给<code>HTTP</code>请求添加<code>Content-Length</code>头部表示请求体的实际长度，也可以由服务器端添加<code>Content-Length</code>返回给客户端，表示响应体内容的实际长度。在实际应用中，<code>Content-Length</code>有时候不好获取，比如实体来自于网络文件或者是有数据库动态产生的，就很难获取它的准确长度。这时候倒是可以开一个足够大的buffer，等内容全部生成完毕在准确计算<code>buffer</code>的总长度，然而这样会导致内存消耗太大，也会让客户端等待时间过长。</p>
            <p>当<code>HTTP</code>请求时短连接的时候，数据传输完成会根据连接是否关闭判断请求体或者响应体的边界，所以不需要标明请求体或者响应体的实际长度，请求就可以顺利完成；而当<code>HTTP</code>为长连接的时候，很显然不可以，必须要拥有判断请求或者响应体的边界，才能完成网络请求，如果<code>Content-Length</code>的值小于实际长度，则数据将会被截断，如果<code>Content-Length</code>的值大于实际长度则会导致网络请求处于停滞状态；如果没有<code>Content-Length</code>或者其他可以判定实体边界，则网络请求还是会被停滞。</p>
            <h3 id="Transfer-Encoding"><a href="#Transfer-Encoding" class="headerlink" title="Transfer-Encoding"></a>Transfer-Encoding</h3>
            <p>使用<code>Transfer-Encoding</code>可以解决我们上面所提到的问题，<code>Transfer-Encoding</code>可以代替<code>Content-Length</code>。<code>Transfer-Encoding</code>表示传输编码，之前文当中给它定义了<code>gzip/compress/deflate/identity/chunked</code>等几种不区分大小写的多种取值，在最新的文档中传输编码之定义了一种编码值：<code>chunked</code>（分块编码）。当我们给请求或者响应的头部添加了<code>Transfer-Encoding:chunked</code>的时候，表示实体在传输的过程中采用的是分块编码的方式，实体被改为一系列的分块，每一个部分分块上面都有数据内容和长度，当分块内容为空长度为0时，意味着实体的边界，数据传输结束。所以，在HTTP网络请求中，如果无法确定实体的<code>Content-Length</code>的大小或者没有添加<code>Content-Length</code>作为头部信息的一部分，会导致网络请求发生问题，此时添加头<code>Transfer-Encoding:chunked</code>（分块编码）可以解决长度问题。</p>
            <h3 id="Accept-Encoding和Content-Encoding"><a href="#Accept-Encoding和Content-Encoding" class="headerlink" title="Accept-Encoding和Content-Encoding"></a>Accept-Encoding和Content-Encoding</h3>
            <p><code>Accept-Encoding</code>添加在请求头中，向服务器端表明客户端支持的编码格式，<code>Content-Encoding</code>存在于响应头中，由服务器端返回，表示响应体内容的编码格式，常见的编码格式有<code>gzip/compress/deflate/identity</code>。可以结合<code>Transfer-Encoding</code>使用。</p>
            <h2 id="举例说明"><a href="#举例说明" class="headerlink" title="举例说明"></a>举例说明</h2>
            <p>再一次<code>HTTP</code>长连接中，无法判断请求体的长度和响应体的长度，我们就会做如下的流程：</p>
            <p>客户端将请求内容加入到请求体中，添加请求头<code>Transfer-Encoding:chunked</code>表明分块编码，同时添加<code>Accept-Encoding:gzip</code>表明客户端支持<code>gzip</code>的编码格式；</p>
            <p>服务器端接收到客户端的网络请求，根据客户端传来的<code>Transfer-Encoding:chunked</code>可知请求体位分块编码，紧接着开始接收请求体数据，当接收到的分块的内容为空长度为0时，说明到了请求体的边界，服务器端完成了数据接收。</p>
            <p>经过一系列的操作，服务器端完了业务处理，给客户端返回数据，由于要返回的数据长度无法获取到，所以服务器端在返回数据中添加了头<code>Transfer-Encoding:chunked</code>表明分块编码，然后将每一块数据内容采用gzip进行编码，同时添加<code>Content-Encoding:gzip</code>表明服务器对返回的数据采用了<code>gzip</code>编码。</p>
            <p>客户端收到服务器端的响应，因为响应头包含了<code>Transfer-Encoding:chunked</code>，所以一直到响应数据的分块长度为0内容为空，才算是数据传输完成，此时客户端会解析服务端发来的<code>Content-Encoding:gzip</code>，并且采用<code>gzip</code>方法解码数据，完成一次数据请求。</p>
            <h1 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h1>
            <p><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_29405933/article/details/84247999">https://blog.csdn.net/qq_29405933/article/details/84247999</a></p>

          </div>





          <footer class="post-footer">
            <div class="post-tags">
              <a href="/tags/HTTP/" rel="tag"># HTTP</a>
              <a href="/tags/Transfer-Encoding/" rel="tag"># Transfer-Encoding</a>
              <a href="/tags/Content-Length/" rel="tag"># Content-Length</a>
              <a href="/tags/Content-Encoding/" rel="tag"># Content-Encoding</a>
              <a href="/tags/Accept-Encoding/" rel="tag"># Accept-Encoding</a>
            </div>



            <div class="post-nav">
              <div class="post-nav-item">
                <a href="/blob/2019-07-03-vary-and-content-negotiation/" rel="prev" title="Vary与内容协商">
                  <i class="fa fa-chevron-left"></i> Vary与内容协商
                </a>
              </div>
              <div class="post-nav-item">
                <a href="/blob/2019-07-16-vue-2.6-was-released/" rel="next" title="Vue 2.6 发布了">
                  Vue 2.6 发布了 <i class="fa fa-chevron-right"></i>
                </a>
              </div>
            </div>
          </footer>
        </article>
      </div>







      <script>
        window.addEventListener('tabs:register', () => {
          let {
            activeClass
          } = CONFIG.comments;
          if (CONFIG.comments.storage) {
            activeClass = localStorage.getItem('comments_active') || activeClass;
          }
          if (activeClass) {
            const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
            if (activeTab) {
              activeTab.click();
            }
          }
        });
        if (CONFIG.comments.storage) {
          window.addEventListener('tabs:click', event => {
            if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
            const commentClass = event.target.classList[1];
            localStorage.setItem('comments_active', commentClass);
          });
        }
      </script>
    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


      <div class="copyright">
        &copy;
        <span itemprop="copyrightYear">2021</span>
        <span class="with-love">
          <i class="fa fa-heart"></i>
        </span>
        <span class="author" itemprop="copyrightHolder">holidaypenguin</span>
      </div>
      <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
      </div>

    </div>
  </footer>


  <script size="300" alpha="0.1" zIndex="-1" src="https://cdn.jsdelivr.net/npm/ribbon.js@1.0.2/dist/ribbon.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
  <script src="/js/love.js"></script>

  <script src="/js/development.js"></script>
  <script src="/js/utils.js"></script>
  <script src="/js/motion.js"></script>
  <script src="/js/schemes/muse.js"></script>
  <script src="/js/next-boot.js"></script>


  <script src="/js/local-search.js"></script>









  <script>
    (function() {
      function leancloudSelector(url) {
        url = encodeURI(url);
        return document.getElementById(url).querySelector('.leancloud-visitors-count');
      }

      function addCount(Counter) {
        const visitors = document.querySelector('.leancloud_visitors');
        const url = decodeURI(visitors.id);
        const title = visitors.dataset.flagTitle;

        Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({
            url
          })))
          .then(response => response.json())
          .then(({
            results
          }) => {
            if (results.length > 0) {
              const counter = results[0];
              leancloudSelector(url).innerText = counter.time + 1;
              Counter('put', '/classes/Counter/' + counter.objectId, {
                  time: {
                    '__op': 'Increment',
                    'amount': 1
                  }
                })
                .catch(error => {
                  console.error('Failed to save visitor count', error);
                });
            } else {
              Counter('post', '/classes/Counter', {
                  title,
                  url,
                  time: 1
                })
                .then(response => response.json())
                .then(() => {
                  leancloudSelector(url).innerText = 1;
                })
                .catch(error => {
                  console.error('Failed to create', error);
                });
            }
          })
          .catch(error => {
            console.error('LeanCloud Counter Error', error);
          });
      }

      function showTime(Counter) {
        const visitors = document.querySelectorAll('.leancloud_visitors');
        const entries = [...visitors].map(element => {
          return decodeURI(element.id);
        });

        Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({
            url: {
              '$in': entries
            }
          })))
          .then(response => response.json())
          .then(({
            results
          }) => {
            for (let url of entries) {
              const target = results.find(item => item.url === url);
              leancloudSelector(url).innerText = target ? target.time : 0;
            }
          })
          .catch(error => {
            console.error('LeanCloud Counter Error', error);
          });
      }

      const {
        app_id,
        app_key,
        server_url
      } = {
        "enable": true,
        "app_id": "povuqNsSqFodlakVIwtEX5kb-gzGzoHsz",
        "app_key": "zXD40RDtDB3DMtpC89k0AK7g",
        "server_url": null,
        "security": false
      };

      function fetchData(api_server) {
        const Counter = (method, url, data) => {
          return fetch(`${api_server}/1.1${url}`, {
            method,
            headers: {
              'X-LC-Id': app_id,
              'X-LC-Key': app_key,
              'Content-Type': 'application/json',
            },
            body: JSON.stringify(data)
          });
        };
        if (CONFIG.page.isPost) {
          if (CONFIG.hostname !== location.hostname) return;
          addCount(Counter);
        } else if (document.querySelectorAll('.post-title-link').length >= 1) {
          showTime(Counter);
        }
      }

      const api_server = app_id.slice(-9) === '-MdYXbMMI' ? `https://${app_id.slice(0, 8).toLowerCase()}.api.lncldglobal.com` : server_url;

      if (api_server) {
        fetchData(api_server);
      } else {
        fetch('https://app-router.leancloud.cn/2/route?appId=' + app_id)
          .then(response => response.json())
          .then(({
            api_server
          }) => {
            fetchData('https://' + api_server);
          });
      }
    })();
  </script>



<script>
            window.imageLazyLoadSetting = {
                isSPA: false,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(n){n.imageLazyLoadSetting.processImages=o;var e=n.imageLazyLoadSetting.isSPA,i=n.imageLazyLoadSetting.preloadRatio||1,r=Array.prototype.slice.call(document.querySelectorAll("img[data-original]"));function o(){e&&(r=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")));for(var t,a=0;a<r.length;a++)0<=(t=(t=r[a]).getBoundingClientRect()).bottom&&0<=t.left&&t.top<=(n.innerHeight*i||document.documentElement.clientHeight*i)&&function(){var t,e,n,i,o=r[a];t=o,e=function(){r=r.filter(function(t){return o!==t})},n=new Image,i=t.getAttribute("data-original"),n.onload=function(){t.src=i,e&&e()},t.src!==i&&(n.src=i)}()}o(),n.addEventListener("scroll",function(){var t,e;t=o,e=n,clearTimeout(t.tId),t.tId=setTimeout(function(){t.call(e)},500)})}(this);</script></body>

</html>